package com.tianyiyan.tyenword;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.tianyiyan.dao.tyenword.UserDao;
import com.tianyiyan.dao.tyenword.UserDao.ResultType;
import com.tianyiyan.hibernate.user.User;

public class UserAction implements Action, SessionAware, ApplicationAware
{
	private User user;
	private Map<String, Object> dataMap = new HashMap<String, Object>();
	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String signup()
	{
		if (user == null)
			return NONE;
		// System.out.println("User: " + user);
		UserDao dao = new UserDao();
		ResultType rType = dao.save(user);
		if (rType == ResultType.SUCCESS)
		{
			dataMap.put("state", "success");
		}
		else
		{
			dataMap.put("state", "error");
			if (rType == ResultType.EMAIL_EXIST)
			{
				dataMap.put("error", "email");
			}
			else if (rType == ResultType.USER_NAME_EXIST)
			{
				dataMap.put("error", "name");
			}
		}
		return SUCCESS;
	}
	
	public String login()
	{
		if (user == null)
			return NONE;
		// System.out.println("User: " + user);
		UserDao dao = new UserDao();
		user = dao.checkUser(user);
		if (user != null)
		{
			if (user.getHasVerification())
			{
				dataMap.put("state", "success");
				session.put("userid", user.getId());
				session.put("username", user.getName());
				Integer count = (Integer)application.get("count");
				if (count == null)
					count = 0;
				count++;
				application.put("count", count);
			}
			else
			{
				session.put("userid", user.getId());
				dataMap.put("state", "noVerification");
			}
		}
		else
			dataMap.put("state", "error");
		return SUCCESS;
	}
	
	public String resendEmail()
	{
		Integer userId = (Integer)session.get("userid");
		if (userId == null)
			dataMap.put("state", "error");
		UserDao dao = new UserDao();
		User u = dao.getUser(userId);
		if (u.getHasVerification())
		{
			dataMap.put("state", "hasVerification");
		}
		else
		{
			dao.resendEmailVerification(u);
			dataMap.put("state", "success");
		}
		return SUCCESS;
	}
		
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String logout()
	{
		Integer count = (Integer)application.get("count");
		if (count != null && count > 0)
		{
			count--;
			application.put("count", count);
		}
		// invalidate ��ʹsessionʧЧ
		((SessionMap)session).invalidate();
		
		return SUCCESS;
	}
	
	private Map<String, Object> session;
	
	private Map<String, Object> application;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setApplication(Map<String, Object> app) {
		application = app;
		
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
